﻿//var jeopardyQuestions = [
//    ["Geography",
//        [
//            [10, "What is today?", "Thursday"],
//            [20, "Who am I?", "Nick"]
//        ]
//    ],
//    ["Languages",
//        [
//            [10, "Language Question 1?", "Language Answer 1"],
//            [20, "Language Question 2?", "Language Answer 2"]
//        ]
//    ]
//]

var jeopardyQuestions = [
                            ["Geography",
                                [
                                    [10, "The 4th Largest City", "Houston"],
                                    [50, "The capitol of Texas", "Austin"],
                                    [100, "The capitol of Zimbabwe", "Harare"],
                                    [400, "The mayor of Pearland", "Tom Reid"]
                                ]
                            ],
                            ["Languages",
                                [
                                    [10, "The language of the web", "javascript"],
                                    [50, "The first language on a mac", "anasewr"],
                                    [100, "The language Apple killed", "Actionscript"],
                                    [400, "The new Apple language", "Swift"]
                                ]
                            ],
                            ["Web People",
                                [
                                    [10, "He made Facebook", "Mark Zuckerberg"],
                                    [50, "He created made Apple", "Steve Jobs"],
                                    [100, "He created Paypal", "Elon Musk"],
                                    [400, "She is CEO of Yahoo", "Marissa Mayer"]
                                ]
                            ]
];

var buildJeopardyTable = function () {
    var htmlArray = [];
    var pointRowObj = {};
    var colNum = 12 / jeopardyQuestions.length;
    var catRowHtml = '';

    if (colNum < 1) {
        return "Invalid boot strap col num";
    }

    var colDiv = '<div class="col-md-' + colNum + ' column">';

    for (var i = 0; i < jeopardyQuestions.length; i++) {
        var categoryName = jeopardyQuestions[i][0];

        //Create the Categories Row
        if (i == 0) {
            catRowHtml = '<div class="row clearfix">';
        }

        //Create a Category Name Column and append to the Category Row
        catRowHtml += colDiv;
        catRowHtml += '<h1><strong>' + categoryName + '</strong></h1>';
        catRowHtml += '</div>';
        
        var categoryQuestions = jeopardyQuestions[i][1];

        for (var j = 0; j < categoryQuestions.length; j++) {
            var questionArray = categoryQuestions[j];

            //Current Point Value
            var pointNum = questionArray[0];
            
            if (!pointRowObj[pointNum]) {
                pointRowObj[pointNum] = '<div class="row clearfix">';
            }
            var pr = pointRowObj[pointNum];
            pr += colDiv;

            for (var k = 0; k < questionArray.length; k++) {
                var value = questionArray[k];
                
                switch (k) {
                    case 0:
                        pr += '<h3>' + value + '</h3><br />';
                        break;
                    case 1:
                        pr += 'Q: ' + value + '<br />';
                        break;
                    case 2:
                        pr += 'A: ' + value + '<br />';
                        break;
                    default:
                        break;
                }
            }
            pr += '</div>';
            pointRowObj[pointNum] = pr;
        }

        if (i == (jeopardyQuestions.length - 1)) {
            catRowHtml += '</div>';
            htmlArray.push(catRowHtml);
        }
    }

    for (var r in pointRowObj) {
        pointRowObj[r] += '</div>';
        htmlArray.push(pointRowObj[r]);
    }
    var h = '';
    for (var i = 0; i < htmlArray.length; i++) {
        h += htmlArray[i];
    }

    document.getElementById('jeopardyTable').innerHTML = h;
}

buildJeopardyTable();